<?php
/**
 * AmplifyCMS
 *
 * This file is part of AmplifyCMS.
 *
 * AmplifyCMS is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * AmplifyCMS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author Rodrigo Rutkoski Rodrigues, <rutkoski@gmail.com>
 * @copyright Copyright 2008 Rodrigo Rutkoski Rodrigues
 */

/**
 * User.
 *
 * @author Rodrigo Rutkoski Rodrigues, <rutkoski@gmail.com>
 */
class User extends Entity
{

  protected static $everyone;

  protected $associations = array(
    array(
      'many-to-many', 'Role', 
      array(
        'table' => 'roles_users', 
        'tableKeyLocal' => 'user_id', 
        'tableKeyForeign' => 'role_id'
      )
    )
  );

  protected $fields = array(
    'id' => array(
      'id', 'int', 
      array(
        'pk' => true, 'autoIncrement' => true
      )
    ), 'username' => array(
      'username', 'varchar'
    ), 'password' => array(
      'password', 'varchar'
    ), 'locale' => array(
      'locale', 'varchar'
    )
  );

  private $roles;

  public function getRoles()
  {
    if ($this->isNew() && ! $this->roles instanceof OutletCollection) {
      $proxy = new User_OutletProxy();
      $this->roles = $proxy->getRoles();
    }
    return $this->roles;
  }

  public function setRoles($roles)
  {
    $this->roles = $roles;
  }

  public function hasLevel($level)
  {
    $roles = $this->getRoles();
    
    foreach ($roles as $role) {
      if ($role->hasLevel($level)) {
        return true;
      }
    }
    
    return false;
  }

  public function is($role)
  {
    $roles = $this->getRoles();
    
    foreach ($roles as $_role) {
      if ($_role->is($role)) {
        return true;
      }
    }
    
    return false;
  }

  public function isAdmin()
  {
    return $this->id == 1;
  }

  public function can($cap)
  {
    if ($this->isAdmin()) return true;
    
    if (empty(self::$everyone)) {
      self::$everyone = OutletDAO::getInstance()->load('Role', 2);
    }
    
    $can = self::$everyone->can($cap);
    if (! is_null($can)) return $can;
    
    $roles = $this->getRoles();
    if (! $roles->count()) return false;
    
    $can = false;
    foreach ($roles as $role) {
      $can = $can ? $can && $role->can($cap) : $can || $role->can($cap);
    }
    
    return $can;
  }

  public function getMinLevel()
  {
    $roles = $this->getRoles();
    $level = PHP_INT_MAX;
    
    foreach ($roles as $role) {
      if ($role->level < $level) $level = $role->level;
    }
    
    return $level;
  }

  public function setPassword($a, $b)
  {
    if (! $this->isNew() && $a == sha1('') && $b == $a) return;
    
    if ($a == sha1('') || $b == sha1('')) {
      $this->errors[] = __('Password can not be empty');
      throw new ValidationException(__('Password can not be empty'));
    }
    
    if ($a != $b) {
      $this->errors[] = __('Passwords do not match');
      throw new ValidationException(__('Passwords do not match'));
    }
    
    $this->set('password', $a);
  }

}

?>